﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharpSoda.Message;
using ServiceStack.Text;

namespace SharpSoda.Node
{
    public class ServiceStackMessageCodec : IMessageCodec
    {
        public ServiceStackMessageCodec()
        {
            JsConfig.DateHandler = DateHandler.ISO8601;
            JsConfig.ThrowOnDeserializationError = true;
        }

        public string ToString(object obj)
        {
            return JsonSerializer.SerializeToString(obj);
        }
        public string ToString<T>(T obj) where T : class
        {
            return JsonSerializer.SerializeToString<T>(obj);
        }
        public T FromString<T>(string str) where T : class
        {
            if (str == null || str.Length <= 0) return null;
            else return JsonSerializer.DeserializeFromString<T>(str);
        }
    }
}
